/// 异常处理
module jinji.util.error;

import std.exception;
public import jinji.util.i18n : _;

private alias Ctors = basicExceptionCtors;

/// 参数错误
class ArgumentException : Exception {
	/// 参数名
	string paramName;

@safe nothrow @nogc:
	this(string msg, string param = null, string file = __FILE__, size_t line = __LINE__,
		Throwable next = null) {
		paramName = param;
		super(_(msg), file, line, next);
	}
}

/// 参数超出范围
class ArgumentOutOfRangeException : ArgumentException {
@safe nothrow @nogc:
	this(string msg, string param = null, string file = __FILE__, size_t line = __LINE__,
		Throwable next = null) {
		super(msg, param, file, line, next);
	}
}

/// 无效操作
class InvalidOperationException : Exception {
	mixin Ctors;
}
/+
/// 未实现
class NotImplementedException : Exception {
	mixin Ctors;
}
+/
alias ArgEx = ArgumentException;
alias ArgOutOfRangeEx = ArgumentOutOfRangeException;

/++
检查参数
Params:
    cond = 条件
    msg = 错误消息
+/
void checkArg(string paramName, bool cond, const scope lazy string msg) {
	if (!cond)
		throw new ArgEx(msg, paramName);
}

/++
检查参数范围
Params:
    cond = 条件
    msg = 错误消息
+/
void checkArgRange(string paramName, bool cond, string msg) {
	if (!cond)
		throw new ArgOutOfRangeEx(msg, paramName);
}

version (Windows) {
	import core.sys.windows.windows;
	import tame.unsafe.string;

	/// 显示错误并退出
	void fatal(string msg) {
		mixin TempWStr!msg;
		FatalAppExit(0, msgw);
	}
}
